<?php

namespace app\admin\controller;

use app\common\controller\BasicAdmin;
use app\common\model\UserModel;
use app\common\traits\SelectPage;
use think\Exception;

class CourseAttendance extends BasicAdmin
{
    use SelectPage;

    protected $model = 'CourseAttendanceModel';

    public function index(){
        $this->title = '学生考勤管理';

        if(input('?attendance_id') && input('attendance_id')){
            $course = model('CourseAttendanceModel')
                ->join('edu_course c','c.id=edu_course_attendance.course_id','INNER')
                ->where('edu_course_attendance.id',input('attendance_id'))
                ->field('edu_course_attendance.*,c.name')
                ->find();

            $this->title = $course['name'];
            $this->assign('course',explode(',',$course['in_userids']));
            $this->assign('set_time',$course['create_time']);

            $data = model('UserModel')->where(function ($db) use ($course){
                $db->where('id','in',$course['all_userids']);
            })->select()->toArray();

            foreach ($data as $k=>$v){
                $user[$k]['attendance'] = in_array($v['id'],explode(',',$course['in_userids']))?2:1;
                $ages[] = in_array($v['id'],explode(',',$course['in_userids']))?2:1;
            }

            array_multisort($ages,SORT_ASC,$data);

            unset($k,$v);
            $ages = [];

            $this->assign('list',$data);

            return $this->fetch('index2');
        }

        if(input('?course_id') && !empty(input('course_id'))){
            $course = model('CourseAttendanceModel')->where('course_id',input('course_id'))->select();
            $this->title = input('course_id_text','社团'.input('course_id'));

            foreach ($course as $value){
                $model = [];
                $model['in_userids'] = explode(',',$value['in_userids']) ;
                $model['name'] =$this->title;
                $model['set_time'] =$value['create_time'];
                $user = model('UserModel')->where(function ($db) use ($value){
                    $db->where('id','in',$value['all_userids']);
                    if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                        $class_id = $this->request->get('class_id');
                        $db->where('class_id', $class_id);
                    }
                    if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                        $grade_id = $this->request->get('grade_id');
                        $db->where('grade_id', $grade_id);
                    }
                })->where('role','=',1)->select()->toArray();

                foreach ($user as $k=>$v){
                    $user[$k]['attendance'] = in_array($v['id'],$model['in_userids'])?2:1;
                    $ages[] = in_array($v['id'],$model['in_userids'])?2:1;
                }

                array_multisort($ages,SORT_ASC,$user);
                $model['user'] = $user;

                if(!empty($model['user'])) $data[] = $model;

                unset($k,$v);
                $ages = [];

                $data[] = $model;
            }
            unset($value);

            $this->assign('list',$data);
            return $this->fetch();
        }else{
            $course = model('CourseModel')->where(function ($db){
//                if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
//                    $school_id = $this->request->get('school_id');
//                    $db->where('school_id', $school_id);
//                }
//                if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
//                    $class_id = $this->request->get('class_id');
//                    $db->where('class_id', $class_id);
//                }
//                if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
//                    $grade_id = $this->request->get('grade_id');
//                    $db->where('grade_id', $grade_id);
//                }
            })->select()->toArray();

            if($course){
                $course = array_column($course,'id');
                $course = implode(',',$course);
            }

            $course_attendance = model('CourseAttendanceModel')
                ->alias('course_attendance')
                ->join('edu_course c','c.id=course_attendance.course_id','left')
                ->where('course_attendance.course_id','in',$course)
                ->order('course_attendance.id DESC')
                ->field('course_attendance.*,c.name')
                ->select();

//                        dump($course_attendance);return;

            $data = [];
            foreach ($course_attendance as $value){
                $model = [];
                $model['in_userids'] = explode(',',$value['in_userids']) ;
                $model['name'] =$value['name'];
                $model['set_time'] =$value['create_time'];
                $user = model('UserModel')->where(function ($db) use ($value){
                    $db->where('id','in',$value['all_userids']);
                    if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
                        $school_id = $this->request->get('school_id');
                        $db->where('school_id', $school_id);
                    }
                    if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                        $class_id = $this->request->get('class_id');
                        $db->where('class_id', $class_id);
                    }
                    if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                        $grade_id = $this->request->get('grade_id');
                        $db->where('grade_id', $grade_id);
                    }
                })->where('role','=',1)->select()->toArray();

                foreach ($user as $k=>$v){
                    $user[$k]['attendance'] = in_array($v['id'],$model['in_userids'])?2:1;
                    $ages[] = in_array($v['id'],$model['in_userids'])?2:1;
                }

                array_multisort($ages,SORT_ASC,$user);
                $model['user'] = $user;

                if(!empty($model['user'])) $data[] = $model;

                unset($k,$v);
                $ages = [];
            }

//            dump($data);return;

            $this->assign('list',$data);

            return $this->fetch();

        }

    }

    public function attendanceList(){
        $c =  $this->_query($this->model)
            ->join('edu_user u','u.id=edu_course_attendance.teacher_id','left')
            ->join('edu_course c','c.id=edu_course_attendance.course_id','left')
            ->where(function ($db){
                if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
                    $status = $this->request->get('school_id');
                    $db->where('u.school_id', $status);
                }
                if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                    $cate_id = $this->request->get('class_id');
                    $db->where('u.class_id', $cate_id);
                }
                if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                    $school_id = $this->request->get('grade_id');
                    $db->where('u.grade_id', $school_id);
                }
                if($this->request->has('course_id','get') && !empty($this->request->get('course_id'))){
                    $school_id = $this->request->get('course_id');
                    $db->where('edu_course_attendance.course_id', $school_id);
                }
            })
            ->field('u.realname,edu_course_attendance.*,c.name AS course_name')
//            ->fetchSql()
            ->page();

        return ($c);
    }

    public function teacher(){


        $c =  $this->_query(model('UserSignInModel'))
            ->join('edu_school school','school.id=edu_user_sign_in.school_id','left')
            ->join('edu_school_class class','class.id=edu_user_sign_in.class_id','left')
            ->join('edu_school_grade grade','grade.id=edu_user_sign_in.grade_id','left')
            ->join('edu_course c','c.id=edu_user_sign_in.course_id','left')
            ->where(function ($db){
                if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
                    $status = $this->request->get('school_id');
                    $db->where('edu_user_sign_in.school_id', $status);
                }
                if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                    $class_id = $this->request->get('class_id');
                    $db->where('edu_user_sign_in.class_id', $class_id);
                }
                if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                    $school_id = $this->request->get('grade_id');
                    $db->where('edu_user_sign_in.grade_id', $school_id);
                }
                if($this->request->has('course_id','get') && !empty($this->request->get('course_id'))){
                    $course_id = $this->request->get('course_id');
                    $db->where('edu_user_sign_in.course_id', $course_id);
                }
            })
            ->field('
                    edu_user_sign_in.*,
                    school.name school_name,
                    class.name class_name,
                    grade.name grade_name,
                    c.name course_name
                ')

            ->page();

        return $c;
    }

}